package com.yonxao.demo.crud.annotation.serializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;

/**
 * 序列化换转换器 <br>
 * 序列化时将 Integer 型数字除以 100 转为浮点型
 *
 * @author xiaosaguo
 * @date 2020/09/08 20:20
 */
public class IntegerDivideHundredJsonSerializer extends JsonSerializer<Object> {

    @Override
    public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
            throws IOException {
        if (o != null) {
            jsonGenerator.writeObject(((int) o) / 100.0);
        } else {
            jsonGenerator.writeObject(null);
        }
    }
}